package com.ww.util.guava;

import com.google.common.base.Joiner;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StringDemo01 {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("hello");
        /**
         * 字符串连接器，以|为分隔符，同时去掉null元素
         */
        Joiner joiner = Joiner.on("|").skipNulls();

        sb = joiner.appendTo(sb, "foo", "bar", null, "baz");
        System.out.println(sb.toString());

        System.out.println("**************************************");

        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(new File("D:/test/tmp.txt"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<Date> dataList = new ArrayList<>();
        dataList.add(new Date());
        dataList.add(null);
        dataList.add(new Date());

        Joiner joiner2 = Joiner.on("#").useForNull("no string");
        try {
            joiner2.appendTo(fileWriter, dataList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
